package com.im.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.im.service.docReview.smo.IDocReviewOperSMO;
import com.im.service.docReview.smo.IDocReviewQuerySMO;
import com.im.service.dto.PageQuery;
import com.im.service.dto.Staff;
import com.im.util.ReturnsUtil;
import com.im.viewmodel.docReview.DocReviewReqVo;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/docReviewService")
public class DocReviewService {
	private static final Logger logger = LogManager.getLogger(DocReviewService.class);
	@Autowired
	private IDocReviewQuerySMO docReviewQuerySmo;
	@Autowired
	private IDocReviewOperSMO docReviewOperSmo;
	
	/**
	 * 标书评审列表查询
	 */
	@RequestMapping(value = "/queryDocReviewList", method = RequestMethod.POST)
	@ResponseBody
	public String queryDocReviewList(@RequestBody String inJson) {
		if (logger.isInfoEnabled()) {
			logger.info("查询标书评审列表请求入参json:{}", inJson);
		}
		try {
			PageQuery<DocReviewReqVo> result = docReviewQuerySmo.queryDocReviewList(inJson);
			if (logger.isInfoEnabled()) {
				JSONObject jo = JSONObject.fromObject(result);
				//logger.info("查询标书评审列表请求出参json:{}", jo.toString());
			}
			return ReturnsUtil.returnSuccess(result, false).toString();
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ReturnsUtil.returnException("查询标书评审列表失败！", e).toString();
		}
	}
	
	/**
	 * 新增标书评审
	 */
	@RequestMapping(value = "/insertDocReviewInfo", method = RequestMethod.POST)
	@ResponseBody
	public String insertDocReviewInfo(HttpServletRequest request,@RequestBody String inJson) {
		if (logger.isInfoEnabled()) {
			logger.info("新增标书评审请求入参json:{}", inJson);
		}
		try {
			HttpSession session = request.getSession();
			Staff staff = (Staff) session.getAttribute("loginedStaff");
			String staffId = staff.getStaffId();
			String result = docReviewOperSmo.insertDocReviewInfo(inJson,staffId);
			if (logger.isInfoEnabled()) {
				logger.info("新增标书评审请求出参json:{}", result);
			}
			return result;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ReturnsUtil.returnException("新增标书评审失败！", e).toString();
		}
	}
	
	/**
	 * 修改标书评审
	 */
	@RequestMapping(value = "/updateDocReviewInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateDocReviewInfo(HttpServletRequest request,@RequestBody String inJson) {
		if (logger.isInfoEnabled()) {
			logger.info("修改标书评审请求入参json:{}", inJson);
		}
		try {
			HttpSession session = request.getSession();
			Staff staff = (Staff) session.getAttribute("loginedStaff");
			String staffId = staff.getStaffId();
			String result = docReviewOperSmo.updateDocReviewInfo(inJson,staffId);
			if (logger.isInfoEnabled()) {
				logger.info("修改标书评审请求出参json:{}", result);
			}
			return result;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ReturnsUtil.returnException("修改标书评审失败！", e).toString();
		}
	}
	
	/**
	 * 上传标书落表
	 */
	@RequestMapping(value = "/insertFileUploadInfoForDocReview", method = RequestMethod.POST)
	@ResponseBody
	public String insertFileUploadInfoForDocReview(HttpServletRequest request,@RequestBody String inJson) {
		if (logger.isInfoEnabled()) {
			logger.info("上传标书落表请求入参json:{}", inJson);
		}
		try {
			HttpSession session = request.getSession();
			Staff staff = (Staff) session.getAttribute("loginedStaff");
			String staffId = staff.getStaffId();
//			String staffId = "123123";
			String result = docReviewOperSmo.insertFileUploadInfoForDocReview(inJson,staffId);
			if (logger.isInfoEnabled()) {
				logger.info("上传标书落表出参json:{}", result);
			}
			return result;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ReturnsUtil.returnException("上传标书落表失败！", e).toString();
		}
	}
	
	/**
	 * 标书评审批量删除
	 */
	@RequestMapping(value = "/deleteBatchDocManage", method = RequestMethod.POST)
	@ResponseBody
	public String deleteBatchDocManage(HttpServletRequest request, @RequestBody String inJson) {
		HttpSession session = request.getSession();
		Staff staff = (Staff) session.getAttribute("loginedStaff");
		String staffId = staff.getStaffId();
		if (logger.isInfoEnabled()) {
			logger.info("标书评审批量删除请求入参json:{}", inJson);
		}
		try {
			String result = docReviewOperSmo.updateBatchDocManage(inJson,staffId);
			if (logger.isInfoEnabled()) {
				logger.info("标书评审批量删除请求出参json:{}", result);
			}
			return result;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ReturnsUtil.returnException("标书评审批量删除失败！", e).toString();
		}
	}

	/**
	 * 查询标书编码是否重复
	 * @param request
	 * @param inJson
	 * @return
	 */
	@RequestMapping(value = "/queryHadDocCode", method = RequestMethod.POST)
	@ResponseBody
	public String queryHadDocCode(HttpServletRequest request, @RequestBody String inJson) {
		if (logger.isInfoEnabled()) {
			logger.info("查询标书编码是否重复请求入参json:{}", inJson);
		}
		try {
			String result = docReviewQuerySmo.queryHadDocCode(inJson);
			if (logger.isInfoEnabled()) {
				logger.info("查询标书编码是否重复请求出参json:{}", result);
			}
			return result;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ReturnsUtil.returnException("查询标书编码是否重复失败！", e).toString();
		}
	}

}
